let express = require('express');

//1.创建路由
let router = express.Router();

//2.路由接口做出响应
router.post('/', (req, res, next) => {
    console.log('user接口路由做出响应')

    //开库
    req.mgdb.open({ collectionName: 'user' }).then(
        ({ collection, client, ObjectId }) => {
            //查找用户名
            // console.log(req.decode.username);
            collection.find({ username: req.decode.username, _id: ObjectId(req.decode._id) }).toArray((err, result) => {
                if (err) { //没有找到库名
                    result.send({ err: 1, msg: '集合操作失误' })
                } else { //找到库名
                    if (result.length > 0) { //判断是否为空
                        //登录成功后就删除当前的用户名和密码,只留下昵称
                        delete result[0].username
                        delete result[0].password
                        res.send({ err: 0, msg: '登录成功', data: result[0] })
                    } else {
                        res.send({ err: 1, msg: '自动登录失败,用户名或者密码错误' })
                    }
                    client.close()
                }
            })
        }
    )

})



//3.导出路由
module.exports = router;